#include "msg_node.h"
#include "../../log.h"

#include <cstdint>

int HeadNode::GetHeatInfo()
{
    if(m_data.get() == nullptr)
    {
        log_error("MsgNode::GetHeatInfo m_data is nullptr");
        return -1;
    }

    if(m_total_len < HEAD_LENGTH)
    {
        log_error("MsgNode::GetHeatInfo m_total_len:{}, HEAD_LENGTH:{}", m_total_len, HEAD_LENGTH);
        return -2;
    }

    memcpy(&m_msg_id, m_data.get(), ID_LENGTH);
    memcpy(&m_msg_len, m_data.get() + ID_LENGTH, MSG_LENGTH);
    m_msg_id = boost::asio::detail::socket_ops::network_to_host_long(m_msg_id);
    m_msg_len = boost::asio::detail::socket_ops::network_to_host_long(m_msg_len);
    log_debug("msg_id:{}, msg_len:{}", m_msg_id, m_msg_len);

    if(m_msg_len > MSG_MAX_LENGTH || m_msg_len == 0)
    {
        log_error("MsgNode::GetHeatInfo msg_len:{} > MSG_MAX_LENGTH:{}", m_msg_len, MSG_MAX_LENGTH);
        return -3;
    }

    if(m_msg_id > MSG_IDS::MSG_MAX_ID)
    {
        log_error("MsgNode::GetHeatInfo msg_id:{} > MSG_MAX_ID:{}", m_msg_id, (uint32_t)MSG_IDS::MSG_MAX_ID);
        return -4;
    }
    
    return 0;
}



SendNode::SendNode(const char *data, uint32_t len, uint32_t msg_id)
:MsgNode(len + HEAD_LENGTH)
,m_msg_id(msg_id)
{
    uint32_t net_id = boost::asio::detail::socket_ops::host_to_network_long(m_msg_id);
    uint32_t net_len = boost::asio::detail::socket_ops::host_to_network_long(len);
    memcpy(m_data.get(), &net_id, ID_LENGTH);
    memcpy(m_data.get() + ID_LENGTH, &net_len, MSG_LENGTH);
    memcpy(m_data.get() + HEAD_LENGTH, data, len);
}